﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GraphDrawer
{
    public partial class DiseaseSelector : UserControl
    {
        private ImportDisease import = new ImportDisease();
        private ModelEditor editor = new ModelEditor();
        private DatabaseModelSelector database = new DatabaseModelSelector();
        private Type diseaseMethod;
        public String GraphML;
        public event RoutedEventHandler isSelectedDisease;
		private static RoutedEventHandler popUpOk;
		private static RoutedEventHandler popUpClosed;

        public DiseaseSelector()
        {
            InitializeComponent();
            isSelectedDisease += new RoutedEventHandler(DiseaseSelector_isSelectedDisease);
        }

        void DiseaseSelector_isSelectedDisease(object sender, RoutedEventArgs e)
        {
            if (diseaseMethod == typeof(ModelEditor))
            {
                GraphML = editor.GraphML;
            }
            if (diseaseMethod == typeof(ImportDisease))
            {
                GraphML = import.modelML;
            }
            if (diseaseMethod == typeof(DatabaseModelSelector)) {
				GraphML = database.modelML;
            }
        }

        private void Database_Click(object sender, RoutedEventArgs e)
        {
			App.Current.Host.Content.IsFullScreen = false;
            database.cl.GetAvailableDiseasesAsync();
			database.Init();
			diseaseMethod = database.GetType();
			popUpOk = new RoutedEventHandler(PopUp_OK);
			popUpClosed = new RoutedEventHandler(PopUp_Closed);
			Menu.PopUp.OK += popUpOk;
			Menu.PopUp.Closed += popUpClosed;
			Menu.PopUp.AddUserControl(database);
			Menu.PopUp.Visibility = Visibility.Visible;
        }

        private void Editor_Click(object sender, RoutedEventArgs e)
        {
			App.Current.Host.Content.IsFullScreen = false;
			
            diseaseMethod = editor.GetType();
            popUpOk = new RoutedEventHandler(PopUp_OK);
			popUpClosed = new RoutedEventHandler(PopUp_Closed);
            Menu.PopUp.OK += popUpOk;
			Menu.PopUp.Closed += popUpClosed;
            Menu.PopUp.AddUserControl(editor);
            Menu.PopUp.Visibility = Visibility.Visible;
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
			App.Current.Host.Content.IsFullScreen = false;
			
            diseaseMethod = import.GetType();
            popUpOk = new RoutedEventHandler(PopUp_OK);
			popUpClosed = new RoutedEventHandler(PopUp_Closed);
            Menu.PopUp.OK += popUpOk;
			Menu.PopUp.Closed += popUpClosed;
            Menu.PopUp.AddUserControl(import);
            Menu.PopUp.Visibility = Visibility.Visible;
        }

        void PopUp_OK(object sender, RoutedEventArgs e)
        {
            Menu.PopUp.OK -= popUpOk;
            isSelectedDisease.Invoke(sender, e);
        }
        
        void PopUp_Closed(object sender, RoutedEventArgs e) {
			Menu.PopUp.OK -= popUpOk;
        }
    }
}
